package test.mvel;

import com.yumingjaing.utils.MvelUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class MvelGetValueExample {

    // 辅助类，用于演示复杂对象
    public static class Product {
        public String name;
        public double price;

        public Product(String name, double price) {
            this.name = name;
            this.price = price;
        }

        // MVEL会通过getter访问，也可以直接访问public字段
        public String getName() { return name; }
        public double getPrice() { return price; }
    }

    public static void main(String[] args) {

        Map<String, Object> ruleContext = new HashMap<>();
        ruleContext.put("userName", "Alice");
        ruleContext.put("userAge", 30);
        ruleContext.put("isActive", true);
        ruleContext.put("productInfo", new Product("Laptop", 1200.0));
        ruleContext.put("addresses", Arrays.asList("123 Main St", "456 Oak Ave", "789 Pine Ln"));
        ruleContext.put("nullableValue", null);

        System.out.println("--- 从 ruleContext 中取值示例 ---");

        // 1. 获取 String 类型的值
        String expression1 = "userName";
        Object result1 = MvelUtil.executeCalculation(expression1, ruleContext);
        System.out.println("表达式: \"" + expression1 + "\" -> 结果: " + result1 + ", 类型: " + result1.getClass().getSimpleName()); // Alice, String

        // 2. 获取 Integer/Number 类型的值
        String expression2 = "userAge";
        Object result2 = MvelUtil.executeCalculation(expression2, ruleContext);
        System.out.println("表达式: \"" + expression2 + "\" -> 结果: " + result2 + ", 类型: " + result2.getClass().getSimpleName()); // 30, Integer

        // 3. 获取 Boolean 类型的值
        String expression3 = "isActive";
        Object result3 = MvelUtil.executeCalculation(expression3, ruleContext);
        System.out.println("表达式: \"" + expression3 + "\" -> 结果: " + result3 + ", 类型: " + result3.getClass().getSimpleName()); // true, Boolean

        // 4. 链式访问复杂对象内部属性 (通过getter或public字段)
        String expression4 = "productInfo.name";
        Object result4 = MvelUtil.executeCalculation(expression4, ruleContext);
        System.out.println("表达式: \"" + expression4 + "\" -> 结果: " + result4 + ", 类型: " + result4.getClass().getSimpleName()); // Laptop, String

        String expression5 = "productInfo.price";
        Object result5 = MvelUtil.executeCalculation(expression5, ruleContext);
        System.out.println("表达式: \"" + expression5 + "\" -> 结果: " + result5 + ", 类型: " + result5.getClass().getSimpleName()); // 1200.0, Double

        // 5. 访问列表/数组元素
        String expression6 = "addresses[1]"; // 获取第二个地址
        Object result6 = MvelUtil.executeCalculation(expression6, ruleContext);
        System.out.println("表达式: \"" + expression6 + "\" -> 结果: " + result6 + ", 类型: " + result6.getClass().getSimpleName()); // 456 Oak Ave, String

        // 6. 使用 Map 的 get() 方法
        String expression7 = "get('userName')";
        Object result7 = MvelUtil.executeCalculation(expression7, ruleContext);
        System.out.println("表达式: \"" + expression7 + "\" -> 结果: " + result7 + ", 类型: " + result7.getClass().getSimpleName()); // Alice, String

        // 7. Null 安全访问 (如果键不存在或值为null)
        // 假设 'nonExistentKey' 不在 ruleContext 中
        String expression8 = "nonExistentKey";
        try {
            Object result8 = MvelUtil.executeCalculation(expression8, ruleContext);
            System.out.println("表达式: \"" + expression8 + "\" -> 结果: " + result8);
        } catch (IllegalArgumentException e) {
            System.out.println("表达式: \"" + expression8 + "\" -> 错误 (预期): " + e.getMessage()); // 预期会抛出异常
        }

        // 使用 ?. 来处理可能不存在的键或为 null 的值
        String expression9 = "nullableValue?.length()"; // 对 null 调用方法
        Object result9 = MvelUtil.executeCalculation(expression9, ruleContext);
        System.out.println("表达式: \"" + expression9 + "\" -> 结果 (null安全): " + result9); // null

        String expression10 = "nonExistentKey?.someProperty"; // 访问不存在的键的属性
        Object result10 = MvelUtil.executeCalculation(expression10, ruleContext);
        System.out.println("表达式: \"" + expression10 + "\" -> 结果 (null安全): " + result10); // null

        // 8. 结合判断逻辑来取值
        String expression11 = "userAge > 25 ? userName : 'Youth'";
        Object result11 = MvelUtil.executeCalculation(expression11, ruleContext);
        System.out.println("表达式: \"" + expression11 + "\" -> 结果: " + result11); // Alice

        ruleContext.put("userAge", 20); // 修改年龄
        Object result12 = MvelUtil.executeCalculation(expression11, ruleContext);
        System.out.println("表达式: \"" + expression11 + "\" (年龄20) -> 结果: " + result12); // Youth
    }
}